<?php

Yii::import('gii.generators.crud.CrudCode');


class YBootstrapCode extends CrudCode
{
	public $modelName;

	public function generateActiveRow($modelClass, $column)
	{
		if ($column->type === 'boolean') {
			return "\$form->checkBoxRow(\$model,'{$column->name}')";
		} else if (stripos($column->dbType, 'text') !== false) {
			return "\$form->textAreaRow(\$model,'{$column->name}',array('rows'=>6, 'cols'=>50, 'class'=>'span8'))";
		} else {
			if (preg_match('/^(password|pass|passwd|passcode)$/i', $column->name)) {
				$inputField = 'passwordFieldRow';
			} else {
				$inputField = 'textFieldRow';
			}

			if ($column->type !== 'string' || $column->size === null) {
				return "\$form->{$inputField}(\$model,'{$column->name}',array('class'=>'span5'))";
			} else {
				return "\$form->{$inputField}(\$model,'{$column->name}',array('class'=>'span5','maxlength'=>$column->size))";
			}
		}
	}

	public function rules()
	{
		return array_merge(parent::rules(), array(
			array('modelName', 'required')
		));
	}

	public function attributeLabels()
	{
		return array_merge(parent::attributeLabels(), array(
			'modelName' => '模型名称',
		));
	}

	public function getControllerFile()
	{
		$controller = YUtil::lcfirst($this->getModelClass());
		if (isset(Yii::app()->backendControllerMap)
			&& isset(Yii::app()->backendControllerMap[$controller])) {
			return Yii::getPathOfAlias(Yii::app()->backendControllerMap[$controller]['class']) . '.php';
		} else {
			return parent::getControllerFile();
		}
	}

	public function getViewPath()
	{
		$controller = YUtil::lcfirst($this->getModelClass());
		if (isset(Yii::app()->backendControllerMap)
			&& isset(Yii::app()->backendControllerMap[$controller])) {
			return dirname(Yii::getPathOfAlias(Yii::app()->backendControllerMap[$controller]['class'])) . '/../views/'.$this->getControllerID();
		} else {
			return parent::getViewPath();
		}
	}
}
